<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style>
    *{
        margin: 0;
        padding: 0;
    }
    .header{
        width: 100%;
        background-color: #eeeeee;
        text-align: center;
        line-height: 200px;
    }
    input{
        padding-left: 4px;
        width: 220px;
        line-height: 30px;
    }
    #s{
        margin-left: 40px;
        margin-right: 40px;
        height: 36px;
        width: 200px;
    }
    button{
        margin-left: 40px;
        width: 200px;
        height: 36px;
        background-color: #007bff;
        color: white;
        border: none;
    }
    .container{
        width: 1200px;
        margin: 0 auto;
    }
    .mian h2{
        margin-top: 10px;
        margin-bottom: 10px;
    }
    .footer h3{
        margin-top: 10px;
        margin-bottom: 10px;
    }
    .footer p{
        display: inline-block;
        width: 32.7%;
        font-size: 18px;
        padding-top: 4px;
        padding-bottom: 4px;
    }
    .footer .ni{
        border-top: 1px solid black;
        border-bottom: 1px solid black;
    }
    #list{
         width:100%;
     }
    #list td{
        width:33%;
    }
</style>
<body>
<div class="header">
    <h1>学员信息</h1>
</div>
<div class="mian">
    <div class="container">
        <h2>添加</h2>
        <input type="text" placeholder="姓名" id="n">
        <select  id="s">
            <option value="男">男</option>
            <option value="女">女</option>
        </select>
        <input type="number" placeholder="年龄" id="a">
        <button id="btn">添加</button>
    </div>
</div>
<div class="footer">
   <div class="container">
       <h3>列表</h3>
      <div class="ni">
          <p>姓名</p>
          <p>性别</p>
          <p>年龄</p>
      </div>
       <table id="list"></table>
   </div>
</div>

<p id="ll"></p>
<script>
    document.querySelector('#btn').addEventListener('click',function () {
        var n = document.querySelector('#n').value;
        console.log(n);
        var s = document.querySelector('#s').value;
        console.log(s);
        var a = document.querySelector('#a').value;
        console.log(a);
        var xmlhttp;
        if (window.XMLHttpRequest){
            xmlhttp=new XMLHttpRequest();
        }
        else{
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                var json =JSON.parse(xmlhttp.responseText);
                console.log(xmlhttp.responseText);
                console.log(json);
                demo()
            }
        };
        xmlhttp.open("GET","http://localhost/ajaxapi/public/index.php/index/student/addStudent?studentName="+n+"&sex="+s+"&age="+a,true);
        xmlhttp.send();
    });
    
    
    function demo() {
        var xmlhttp;
        if (window.XMLHttpRequest){
            xmlhttp=new XMLHttpRequest();
        }
        else{
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                var json =JSON.parse(xmlhttp.responseText);
                console.log(xmlhttp.responseText);
                console.log(json);
                document.querySelector("#list").innerHTML="";
                for(var i = 0;i<json.data.length;i++){
                    var tr = document.createElement("tr");
                    var name = document.createElement("td");
                    name.innerHTML = json.data[i].studentName;
                    var sex = document.createElement("td");
                    sex.innerHTML = json.data[i].sex;
                    var age = document.createElement("td");
                    age.innerHTML = json.data[i].age;
                    tr.appendChild(name);
                    tr.appendChild(sex);
                    tr.appendChild(age);
                    document.querySelector("#list").appendChild(tr)
                }
            }
        };
        xmlhttp.open("GET","http://localhost/ajaxapi/public/index.php/index/student/student",true);
        xmlhttp.send();
    }
    demo()
</script>
</body>
</html>